package com.jmm.api.controller.user;

import com.jmm.api.pojo.dto.login.UserLoginDTO;
import com.jmm.api.pojo.vo.login.TokenVO;
import com.jmm.api.service.user.UserService;
import com.jmm.common.annotation.Log;
import com.jmm.common.constant.ApiRoleConstants;
import com.jmm.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jmm.common.utils.Result;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


/**
 * 用户登录绑定
 */
@RestController
@RequestMapping("/api/user/")
@Api(tags = { "用户控制层" })
public class ApiUserController{
    @Autowired
    private UserService userService;

    @PostMapping("login")
    @Log("api测试-登录")
    @ApiOperation("api测试-登录")
    public Result<?> login(@RequestBody final UserLoginDTO loginDTO) {
        TokenVO token = userService.getToken(loginDTO.getPhone(), loginDTO.getPasswd(),loginDTO.getSocialToken());
        return Result.ok(token);
    }

    @PostMapping("smslogin")
    @Log("api测试-短信登录")
    @ApiOperation("api测试-短信登录")
    public Result<?> smslogin(@RequestBody final UserLoginDTO loginDTO) {
        TokenVO token = userService.getSmsToken(loginDTO.getPhone(), loginDTO.getSmscode(),loginDTO.getSocialToken());
        return Result.ok(token);
    }

    @PostMapping("register")
    @Log("api测试-注册")
    @ApiOperation("api测试-注册")
    public Result<?> register(@RequestBody final UserLoginDTO loginDTO) {
        userService.register(loginDTO.getPhone(), loginDTO.getPasswd(),loginDTO.getSmscode());
        return Result.ok();
    }
    @PostMapping("forget")
    @Log("api测试-忘记密码")
    @ApiOperation("api测试-忘记密码")
    public Result<?> forget(@RequestBody final UserLoginDTO loginDTO) {
        userService.forget(loginDTO.getPhone(), loginDTO.getPasswd(),loginDTO.getSmscode());
        return Result.ok();
    }
    @PostMapping(value = "refresh",consumes = "application/text" )
    @Log("api测试-刷新token")
    @ApiOperation("api测试-刷新token")
    public Result<?> refresh(@RequestParam String phone, @RequestBody final String refreshToken) {
        TokenVO token = userService.refreshToken(phone, refreshToken);
        return Result.ok(token);
    }

    @GetMapping("/require_auth")
    @RequiresAuthentication
    @Log("api测试-需要认证才能访问")
    @ApiOperation("api测试-需要认证才能访问")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    public Result<?> requireAuth() {
        return Result.build(200, "认证通过", null);
    }

    @GetMapping("/require_role")
    @RequiresRoles(ApiRoleConstants.MANAGER)
//    @Log("api测试-需要api角色才能访问")
    @ApiOperation("api测试-需要api角色才能访问")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    public Result<?> requireRole() {
        return Result.build(200, "用户有role角色权限", null);
    }

    @GetMapping("/require_permission")
    @RequiresPermissions("api:user:update")
//    @Log("api测试-需要api:user:update权限才能访问")
    @ApiOperation("api测试-需要api:user:update权限才能访问")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    public Result<?> requirePermission() {
        return Result.build(200, "用户有api:user:update权限", null);
    }

    @PostMapping("/checkPhone")
    @RequiresAuthentication
    @Log("验证用户合法性")
    @ApiOperation("绑定前校验验证码")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    public Result<?> toCheckPhone(@RequestBody final UserLoginDTO loginDTO){
        userService.changePhoneCheck(ShiroUtils.getUserId(),loginDTO.getSmscode());
        return Result.ok();
    }

    @PostMapping("/bindingPhone")
    @RequiresAuthentication
    @Log("执行绑定手机")
    @ApiOperation("绑定手机")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    public Result<?> bindingPhone(@RequestBody final UserLoginDTO loginDTO){
        userService.bindingPhone(ShiroUtils.getUserId(),loginDTO.getPhone(),loginDTO.getSmscode());
        return Result.ok();
    }

}
